<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="data.queryParams" :inline="true">
            <el-form-item label="模板名称" prop="roleName">
              <el-input v-model="data.queryParams.realName" placeholder="请输入模板名称" clearable
                @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="hover">
      <template #header>
        <el-row :gutter="10">

          <el-col :span="1.5">
            <el-button type="danger" plain @click="handleDelete" :disabled="selectionList.length === 0">删除</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table ref="roleTableRef" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="模板名称" prop="realName" :show-overflow-tooltip="true" />
        <el-table-column label="模板简介" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="创建人" prop="createBy" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="data.queryParams.total > 0" v-model:page="data.queryParams.pageNum"
        v-model:limit="data.queryParams.pageSize" :total="data.queryParams.total" @pagination="getList" />

    </el-card>
  </div>
</template>

<script setup lang="ts">
import { apiEnterpriseTemplateList, apiEnterpriseTemplateDel } from '@/api/enterprise/enterpriseTemplate';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const loading = ref(true);
const showSearch = ref(true);
const dataList = ref([]);
const selectionList = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    total: 0,
    realName: '',
  },
});



/**
 * 查询模板列表
 */
const getList = async () => {
  loading.value = true;
  const res: any = await apiEnterpriseTemplateList({
    pageNum: data.queryParams.pageNum,
    pageSize: data.queryParams.pageSize,
    realName: data.queryParams.realName,
  });
  if (res.code == 200) {
    loading.value = false;
    dataList.value = res.data.rows;
    data.queryParams.total = res.data.total;
  }
};

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  data.queryParams.pageNum = 1;
  getList();
};

/** 重置 */
const resetQuery = () => {
  // dateRange.value = ['', ''];
  // queryFormRef.value?.resetFields();
  // handleQuery();
};
const handleSelectionChange = (val) => {
  selectionList.value = val;
}
/**删除按钮操作 */
const handleDelete = async () => {
  ElMessageBox.confirm('此操作将永久删除该模板, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const ids = selectionList.value.map((item) => item.id);
    const res = await apiEnterpriseTemplateDel(ids);
    if (res.code == 200) {
      ElMessage.success('删除成功');
      getList();
    }
  }).catch(() => {
    ElMessage.info('已取消删除');
  });
};


onMounted(() => {
  getList();
});
</script>
